<template>
  <div class="sift">
    <div class="tag">
      添加学生
    </div>
    <br/>
    <br/>
    <x-input label-width="90px" title="学生姓名" v-model="name"></x-input>
    <x-input label-width="90px" title="学校" v-model="school"></x-input>
    <popup-radio title="年级" :options="slots" v-model="class_" placeholder="请选择"></popup-radio>
    <br/>
    <br/>
    <br/>
    <x-button type="primary" @click.native="save">保存</x-button>
  </div>
</template>

<script>
import SiftItem from "@/components/base/sift-item/sift-item";
import { XInput, XButton, PopupRadio } from "vux";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      pageInfo: {
        currentPage: 0,
        hasMore: false
      },
      books: [],
      slots: [
        {
          key: "1",
          value: "一年级"
        },
        {
          key: "2",
          value: "二年级"
        },
        {
          key: "3",
          value: "三年级"
        },
        {
          key: "4",
          value: "四年级"
        },
        {
          key: "5",
          value: "五年级"
        },
        {
          key: "6",
          value: "六年级"
        }
      ],
      name: "",
      school: "",
      class_: "1"
    };
  },
  components: {
    SiftItem,
    XInput,
    XButton,
    PopupRadio
  },
  mounted() {
    this.$$api_book_articleList({
      data: {
        page: this.pageInfo,
        currentPage: 0
      },
      fn: res => {
        this.books = res;
      }
    });
  },
  methods: {
    goBack() {
      this.$emit("back");
    },
    save() {
      let _this = this;
      if (!this.name) return this.$vux.toast.text("请输入名字", "center");
      if (!this.school) return this.$vux.toast.text("请输入学校", "center");
      this.$$api_user_add_student({
        data: {
          name: this.name,
          school: this.school,
          class: this.class_,
          user_id: this.userID,
          word_count: 0,
          gold_coin: 0,
          is_default: 1
        },
        fn: res => {
          
          this.$store.dispatch("update_student", res.studentId);
          this.studentId = res.studentId;
          this.$$api_user_query_default_student({
            data: this.userID,
            fn: res => {
              this.$store
                .dispatch("update_default_student", res)
                .then(result => {
                  this.$emit("back");
                  //  this.$router.replace(`/find`);
                });
            }
          });
        },
        errFn: res => {
          // console.log(res);
        }
      });
      // this.goBack();
    }
  },
  computed: {
    ...mapGetters(["userID"])
  }
};
</script>

<style lang="scss" scoped>
@import "~@/assets/scss/const.scss";
@import "~@/assets/scss/mixin.scss";

.sift {
  background: $app-background-color;
  position: fixed;
  z-index: 100;
  top: 0;
  left: 0;
  height: 100vh;
  width: 100vw;
  overflow: auto;
  .tag {
    width: 100%;
    height: 40px;
    line-height: 40px;
    color: rgba(0, 0, 0, 1);
    text-align: center;
    font-size: 14px;
    font-weight: bold;
    background: #f2f2f2;
  }
  .icon-back {
    position: fixed;
    top: 10px;
    left: 10px;
    font-size: 40px;
    color: #999999;
  }
  select {
    min-height: 48px;
    line-height: 48px;
    padding: 0 6px;
    width: 100%;
    border: none;
  }
  button {
    width: 94%;
    margin-left: 3%;
  }
}
</style>
